<template>
  <div class="app-container">
    <!-- 添加或修改发行管理对话框 -->
    <el-dialog :title="title" :visible.sync="visible" width="800px" v-if="visible" append-to-body
      :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth" :disabled="disabled">
        <el-form-item label="白名单用户：" prop="">
          <el-upload class="upload-demo" :action="fileaction" :headers="headers" :data="fileData" :limit="1"
            :on-success="handleFileChange" :show-file-list="true" :file-list="fileList">
            <el-button type="primary" size="small">导入用户</el-button>
            <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
          </el-upload>
        </el-form-item>
        <el-form-item label="用户手机号：" prop="">
          <el-input v-model="form.phone" placeholder="请输入用户手机号" :style="{width: inputWidth}" />
          <el-button type="primary" size="small" style="margin-left: 20px" @click="addUser" :loading="addLoading">添 加
          </el-button>
        </el-form-item>
      </el-form>
      <div class="tabel_box">
        <div class="tabel_box_h">
          <el-button type="danger" @click="updateWhiteList" :loading='whiteListLoading'>更新白名单</el-button>
          <div  style="margin-left: 10px" v-if="tabelList.length>0">
            <export-excel-button title="白名单" action="/equityManager/equityCardIssue/exportWhite"
              :params="ExportParams" />
          </div>
        </div>
        <el-table :data="tabelList" v-loading="loading" element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.6)" element-loading-text="加载中" stripe border>
          <el-table-column label="序号" align="center" type="index" width="64px" />
          <el-table-column label="用户姓名" align="center" prop="name" :show-overflow-tooltip="true" />
          <el-table-column label="手机号" align="center" prop="phone" :show-overflow-tooltip="true" />
          <el-table-column label="校验结果" align="center" prop="time" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span class="el-icon-success icon_yes" v-if="scope.row.name"></span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="deleteView(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 导入失败弹窗 -->
      <el-dialog title="导入失败" :visible.sync="dialogVisible" width="500" append-to-body>
        <div>{{importMessage}}</div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getToken
  } from "@/utils/auth";
  import {
    addWhite,
    whiteList,
    deleteWhite,
    refreshWhite
  } from "@/api/equityManager/equityCardIssue";
  import ExportExcelButton from "@/components/ExportExcelButton/index";
  export default {
    // dicts: ['asset_card_type'],
    name: "dialogwhiteList",
    components: {
      ExportExcelButton,
    },
    data() {
      return {
        labelWidth: '120px',
        inputWidth: '200px',
        whiteListLoading: false,
        dialogVisible: false, //导入失败弹窗
        importMessage: '', //导入失败原因
        id: '', //
        loading: false,
        addLoading: false,
        // 导入所需参数
        fileData: {
          issueId: ''
        },
        fileList: [],
        fileaction: process.env.VUE_APP_BASE_API + "/equityManager/equityCardIssue/importWhite",
        headers: {
          "X-Access-Token": getToken(),
        }, //设置上传文件请求头
        // 是否禁用表单
        disabled: false,
        // 弹出层标题
        title: "优先抢购白名单",
        // 是否显示弹出层
        visible: false,
        //白名单数组
        tabelList: [],
        // 表单参数
        form: {
          issueId: '', //发行id
          phone: '', //用户手机号
        },
        // 表单校验
        rules: {
          phone: [{
            required: true,
            message: "请填写手机号",
            trigger: ["blur", "change"]
          }],
        },
      };
    },
    created() {

    },
    computed: {
      ExportParams() {
        const {
          id: ticketId
        } = this;
        const params = {
          issueId: this.form.issueId,
        };
        return params;
      },
    },
    methods: {
      //获取白名单列表
      getWhiteList(id) {
        this.loading = true
        let data = {
          issueId: id
        }
        whiteList(data).then(response => {
          this.tabelList = response.result
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      /** 打开白名单弹窗 */
      open(id) {
        this.reset();
        this.fileData.issueId = id
        this.form.issueId = id
        this.getWhiteList(id)
        this.visible = true
      },
      // 取消按钮
      cancel() {
        this.visible = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form.phone = ''
      },
      /** 删除提交按钮deleteWhite */
      deleteView(row) {
        this.$confirm('是否删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteWhite(row.id).then(response => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getWhiteList(this.form.issueId)
          });

        }).catch(() => {});
      },
      //导入文件
      handleFileChange(res, file) {
        console.log('文件', res, file)
        if (res.code == 200) {
          this.$message.success('导入成功');
          this.getWhiteList(this.form.issueId)
        } else {
          // this.$message.error(res.message);
          this.importMessage = res.message
          this.dialogVisible = true
          setTimeout(() => {
            this.fileList = []
          }, 800)
        }
      },
      /** 添加按钮 */
      addUser() {
        this.addLoading = true
        addWhite(this.form).then(response => {
          this.$message.success('添加成功');
          this.getWhiteList(this.form.issueId)
          this.form.phone = ''
          this.addLoading = false
        }).catch(() => {
          this.addLoading = false
        });
      },
      /** 更新白名单 */
      updateWhiteList(){
        this.whiteListLoading = true
        var data = {
          issueId: this.form.issueId
        }
        refreshWhite(data).then(response => {
          this.$message.success('更新成功');
          this.whiteListLoading = false
        }).catch(() => {
          this.whiteListLoading = false
        });
      },
      /** 提交按钮 */
      //   submitForm() {
      //     this.$refs["form"].validate(valid => {
      //       if (valid) {
      //         if (this.form.issueType == '1') { //直接购买
      //           this.form.startTime = this.form.date[0]
      //           this.form.endTime = this.form.date[1]
      //         } else { //预约购买
      //           this.form.startTime = this.form.dateTwo[0]
      //           this.form.endTime = this.form.dateTwo[1]
      //         }
      //         if (this.form.id != null) {
      //           updateEquityCardIssue(this.form).then(response => {
      //             this.$modal.msgSuccess("修改成功");
      //             this.cancel();
      //             this.$emit('refresh')
      //           }).finally(() => {
      //             this.$modal.closeLoading();
      //           });
      //         } else {
      //           addEquityCardIssue(this.form).then(response => {
      //             this.$modal.msgSuccess("新增成功");
      //             this.cancel();
      //             this.$emit('refresh')
      //           }).finally(() => {
      //           });
      //         }
      //       }
      //     });
      //   },
    }
  };

</script>
<style scoped>
  .tabel_box_h {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-bottom: 10px;
  }

  .icon_yes {
    font-size: 30px;
    color: #67c23a;
  }

  .icon_no {
    font-size: 30px;
    color: #f56c6c;
  }

</style>
